from datetime import datetime

# 用户模型类
class UserModel:
    def __init__(self, id=None, openid=None, session_key=None, unionid=None, 
                 nickname=None, avatar_url=None, points=None, last_study_time=None, 
                 created_at=None, updated_at=None):
        self.id = id
        self.openid = openid  # 微信用户唯一标识
        self.session_key = session_key  # 微信会话密钥
        self.unionid = unionid  # 同一用户在不同小程序下的唯一标识（需满足一定条件）
        self.nickname = nickname  # 用户昵称
        self.avatar_url = avatar_url  # 用户头像URL
        self.points = points or 0  # 用户积分
        self.last_study_time = last_study_time  # 最后学习时间
        self.created_at = created_at or datetime.now()  # 创建时间
        self.updated_at = updated_at or datetime.now()  # 更新时间
    
    def to_dict(self):
        """
        将模型对象转换为字典
        注意：不返回敏感信息如session_key
        """
        result = {
            'id': self.id,
            'openid': self.openid,
            'nickname': self.nickname,
            'avatar_url': self.avatar_url,
            'points': self.points,
            'last_study_time': self.last_study_time.isoformat() if isinstance(self.last_study_time, datetime) else self.last_study_time,
            'created_at': self.created_at.isoformat() if isinstance(self.created_at, datetime) else self.created_at,
            'updated_at': self.updated_at.isoformat() if isinstance(self.updated_at, datetime) else self.updated_at
        }
        return result
    
    def to_dict_with_session(self):
        """
        将模型对象转换为字典（包含session_key）
        仅在内部使用，不对外返回
        """
        data = self.to_dict()
        data['session_key'] = self.session_key
        data['unionid'] = self.unionid
        return data